//面包屑
// new Vue().$mount('#crumbs');
function setDate(row){
	var orgname = row.orgname;//部门名称
	var deptQuota = row.deptQuota;//已分配短信数
	$($('.el-input__inner')[0]).val(orgname);//部门
	$($('.el-input__inner')[1]).val(deptQuota);//分配可用短信数
    
    //var orgCode = row.orgCode;//组织机构编号
    var deptQuotaId = row.deptQuotaId;
    $("#deptQuota").val(deptQuota);
    $("#deptQuotaId").val(deptQuotaId);
}
function setDate1(){
	var orgname = $('#orgname').val();//部门名称
	$($('.el-input__inner')[1]).val(orgname);//部门
}

var sm = new Vue({
    el: "#sendedmessage",
    data: function () {
        return {
            loading:false,
            time: '',
            btime: '',
            etime: '',
            keyword: '',
            tableData: [],
            total: 0,
            pageSize: 10,
            pageIndex: 1,
            sels: [],
            visible: true
        }
    },
    methods: {
        search: function () {
            var times = this.time;
            if (times != null && times != '') {
                times = times.toString().split(",");
                this.btime = dateFormat(times[0]);
                this.etime = dateFormat(times[1]);
            }
            console.log("time:" + this.time);
            console.log("bime:" + this.btime);
            console.log("eime:" + this.etime);
            console.log("keyword:" + this.keyword);
            this.getData(1, this.pageSize, this.keyword, this.btime, this.etime);
        },

        sendMessage: function () {
            $("#content").load("templates/authority/sendMessage/sendMessage.html");
        },
        editMessage: function (row) {
        	document.cookie = "msgId="+row.msgId;
            $("#content").load("templates/authority/sendMessage/editMessage.html");
        },
        deleteMsg: function(row){
        	if (this.sels.length === 0) {
                this.$alert('请选择要删除的信息。', '提示', {
                    type: 'warning',
                    customClass: "notLoadingThis"
                });
                return false;
            }            
            this.deleteMsg(row.msgId);
        },
        batchDelete: function () {
            if (this.sels.length === 0) {
                this.$alert('请选择要删除的信息。', '提示', {
                    type: 'warning',
                    customClass: "notLoadingThis"
                });
                return false;
            }
            var msgIds = this.sels.map(function (msg) {
                return msg.msgId;
            }).join(",");
            console.log("msgIds:" + msgIds);
            this.deleteMsg(msgIds);
        },

        deleteMsg: function (msgIds) {
            var self = this;
            this.$confirm('确认删除所选信息？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function () {
                VanyAjax("/sendedmsg/batchDeleteMsg", {
                    msgIdList: msgIds
                }).done(function (rspdata) {
                    this.getData(this.pageIndex, this.pageSize, this.keyword, this.btime, this.etime);
                }, true, self);
            });
        },

        getMsgDetail: function (row) {
            $("#content").load("templates/authority/timedMesage/messageDetail.html?msgId=" + row.msgId);
        },
        editMsgPop: function(row){
        	$('#popview').click();      
        	setTimeout(function(){setDate(row)},500); //(function(){fun(a,b,c)},500
        	//var monthlyQuota = row.monthlyQuota;//部门名称       	
        	
        },
        editMsgPop1: function(){
        	$('#popview1').click();              	
        },
        MsgPop_sure: function(){//确认操作       	
        	$('#cancel').click();  
        	$.ajax({
        		url: "/msgStatistic/editSmsDistrLimit",
        		method: "POST",
        		data:{token:sessionStorage.token,deptQuota:$($('.el-input__inner')[1]).val(),deptQuotaId:$("#deptQuotaId").val()},
        		success: function(data) {
        			this.getData(this.pageIndex, this.pageSize, this.keyword, this.btime, this.etime);
        		}
        	});
        	/*VanyAjax("/msgStatistic/editSmsDistrLimit", {
        		token:sessionStorage.token,
        		deptQuota:$($('.el-input__inner')[1]).val(),
        		deptQuotaId:$("#deptQuotaId").val()
            }).done(function (rspdata) {
                this.getData(this.pageIndex, this.pageSize, this.keyword, this.btime, this.etime);
            }, true, self);*/
  	
        },
        MsgPop_cancel: function(){//取消操作
        	$('#popview').click();
        },
        editMsgPop1: function(){
        	$('#popview1').click();
        },
        MsgPop_sure1: function(){//确认操作
        	$('#popview1').click();
        	setTimeout(function(){setDate1()},500); 
        },
        MsgPop_cancel1: function(){//取消操作
        	$('#popview1').click();
        },
        statisticMsg: function (row) {
            alert(row);
        },
        editSms:function(){//底部编辑
    		if (this.sels.length === 0) {
                this.$alert('请选择一行信息。', '提示', {
                    type: 'warning',
                    customClass: "notLoadingThis"
                });
                return false;
            }
            var deptQuotaId = this.sels.map(function (msg) {
                return msg.deptQuotaId;
            });    
            var orgname = this.sels.map(function (msg) {
                return msg.orgname;
            });  
            $('#deptQuotaId').val(deptQuotaId);
            $('#orgname').val(orgname);
            this.MsgPop_sure1();
    	},
        getData: function (pageIndex, pageSize, keyword, btime, etime) {
            VanyAjax("/msgStatistic/getSmsDistrInfoById", {
                pageIndex: pageIndex,
                pageSize: pageSize,
                keyWord: keyword,
                token:sessionStorage.token,
                startTime: btime,
                endTime: etime
            }, "POST").done(function (data) { 
                this.currentPage = data.pageNum;
                this.pageSize = 10;
                this.total = data.total;
                var msgs = data.rows;
                this.tableData = msgs;
                console.log(msgs.length);
                for (var i = 0; i < msgs.length; i++) {                	
                	if(i==0){
                    	console.log(msgs[i].receivers);
                    }
                    if(i==2){
                    	console.log(msgs[i].receivers);
                    }
                    this.tableData[i].sendTime = new Date((msgs[i].sendTime)).Format("yyyy-MM-dd hh:mm:ss")
                    
                    var receivers = eval('(' + msgs[i].receivers + ')');
                    
                    var names = "";
                    for (var j = 0; j < receivers.length; j++) {
                        if (j == 0) {
                            names = receivers[j].name;
                        } else {
                            names = names + "," + receivers[j].name;
                        }
                    }
                    this.tableData[i].receivers = names;
                    console.log(i);
                }
                this.loading = false;

            }, false, this);
        },

        handleCurrentChange: function (val) {
            console.log('当前页:' + val);
            this.getData(val, this.pageSize, this.keyword, this.btime, this.etime);
        },

        handleSelectionChange: function (selection) {
            this.sels = selection;
        },

        handleSizeChange: function(val){
            this.getData(this.pageIndex, val, this.keyword, this.btime, this.etime);
        }
    },
    mounted: function () {
        this.getData(this.pageIndex, this.pageSize, this.keyword, this.btime, this.etime);
    },
})
Date.prototype.Format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1,                 //月份
        "d+": this.getDate(),                    //日
        "h+": this.getHours(),                   //小时
        "m+": this.getMinutes(),                 //分
        "s+": this.getSeconds(),                 //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds()             //毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};
dateFormat = function (d) {
    var date = new Date(d);
    return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
}
getNumbyStatus = function(){

}